<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2020/7/15
  Time: 18:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <script>
        //防止二次加载失败
        function backHtml(){
            aouDivHtml = jqueryId("aouDiv").html();
        }


        //ajax重新加载数据
        function query(){
            datatable.ajax.reload();
        }

        //批量删除
        function bothDelete(){
            var arr = [];
            $("[name=productIds]:checked").each(function(){
                arr.push($(this).val());
            })
            if (arr.length>0){
                bootBoxConfirm("你确定要"+arr.length+"删除吗？","批量删除品牌",
                    function(result){
                        console.log(result);
                        if(result)
                        {
                            jqueryAjax(
                                "<%=basePath%>product/bothdeleteProduct.do",
                                {
                                    arr:arr,

                                },
                                function(result){
                                    if(result.code == 0)
                                    {
                                        bootBoxAlert(result.msg);
                                        query();//重新加载datatables
                                    }
                                }
                            )
                        }


                    }
                )
            }

        }
        //删除
        function deleteProduct(productId){
            bootBoxConfirm("你确定要删除吗？","删除品牌",
                function(result){
                    console.log(result);
                    if(result)
                    {
                        jqueryAjax(
                            "<%=basePath%>product/deleteProduct.do",
                            {
                                productId:productId,

                            },
                            function(result){
                                if(result.code == 0)
                                {
                                    bootBoxAlert(result.msg);
                                    query();//重新加载datatables
                                }
                            }
                        )
                    }


                }
            )
        }
        //显示修改页面
        function showUpdate(productId){
            jqueryAjax("<%=basePath%>/product/toUpdateProduct.do",{productId:productId},
                function(result){
                    console.log(result);
                    if(result.code==0){

                        jqueryName("aouProductId").val(result.data.productId);
                        jqueryName("aouProductName").val(result.data.productName);
                        jqueryName("aouproductPrice").val(result.data.productPrice);
                        jqueryName("aouproductStock").val(result.data.productStock);
                        jqueryName("aouProductLogo").val(result.data.productMainImage);

                        jqueryName("aouproductStatus").each(function(){
                            if($(this).val() == result.data.productStatus){
                                $(this).prop("checked",true);
                            }
                        });
                        jqueryName("aouProductHot").each(function(){
                            if($(this).val() == result.data.productIsHot){
                                $(this).prop("checked",true);
                            }
                        });

                        initFileInput("fileImg","aouProductLogo",["/mall/"+result.data.productLogo])

                        bootBoxConfirm(jqueryId("aouDiv form"),"修改品牌",
                            function(type){ //确定才修改
                                if(type)
                                {

                                    jqueryAjax(
                                        "<%=basePath%>product/updateProduct.do",
                                        {
                                            productId:jqueryName("aouProductId").val(),
                                            productName:jqueryName("aouProductName").val(),
                                            productPrice:jqueryName("aouproductPrice").val(),
                                            productStock:jqueryName("aouproductStock").val(),
                                            productStatus:jqueryName("aouproductStatus",true).val(),
                                            productIsHot:jqueryName("aouProductHot",true).val(),
                                            productMainImage:jqueryId("aouProductLogo").val(),
                                        },
                                        function(result){
                                            if(result.code == 0)
                                            {
                                                bootBoxAlert(result.msg);
                                                query();//重新加载datatables
                                            }
                                        }
                                    )


                                }



                            }
                        )



                    }


                }
            )
            jqueryId("aouDiv").html(aouDivHtml);//恢复html结构
        }

        //新增
        function showAdd(){
            bootBoxConfirm(
                jqueryId("aouDiv form"),
                "新增品牌",
                function(result){
                    console.log(result);

                    if(result)
                    {
                        jqueryAjax(
                            "<%=basePath%>product/addProduct.do",
                            {
                                productName:jqueryName("aouProductName").val(),
                                productPrice:jqueryName("aouproductPrice").val(),
                                productStock:jqueryName("aouproductStock").val(),
                                productStatus:jqueryName("aouproductStatus",true).val(),
                                productIsHot:jqueryName("aouProductHot",true).val(),
                                productMainImage:jqueryId("aouProductLogo").val(),
                            },
                            function(result){
                                if(result.code == 0)
                                {
                                    bootBoxAlert(result.msg);
                                    query();//重新加载datatables
                                }
                            }
                        )
                    }


                    jqueryId("aouDiv").html(aouDivHtml);//恢复html结构
                }
            )
            initFileInput("fileImg","aouProductLogo");//初始化上传控件
        }

        //初始化datatables
        function initDatatables(tableId,url,data,columns){
            return jqueryId(tableId).DataTable({
                //开启服务端模式
                serverSide:true,
                //是否显示加载中
                processing:true,
                //开启是否显示搜索框(我们自己做条件查询)
                searching:false,
                //设置每页显示条数下拉框的值
                lengthMenu:[5,10,20],
                //是否开启排序功能
                ordering: true,
                //指定某些列不使用排序
                //columnDefs: [ {orderable: false, targets: [0,1,2]}],
                //隔行换色//根据返回数据,设置某行某列样式
                createdRow: function ( row, data, index ) {
                    //console.log(data);
                    // if ( index %2 == 0 ) {
                    //     $('td', row).css('font-weight',"bold").css("color","red");
                    // }
                },
                language: {//指定国际化资源文件
                    "url": "<%=basePath%>js/DataTables/Chinese.json"
                },
                ajax:{
                    //设置请求的地址
                    "url":url,
                    "type":"post",
                    "data":data,
                    dataSrc:function (result) {//ajax的返回函数,返回值打印控制台,方便打印控制台,进行调试
                        console.log("datatable 返回的数据:");
                        console.log(result);
                        if(result.code == 0)//成功
                        {
                            result.draw = result.data.draw;
                            result.recordsFiltered = result.data.recordsFiltered;
                            result.recordsTotal = result.data.recordsTotal;
                            return result.data.data
                        }
                        return {};
                    }
                },
                //设置列
                columns:columns
            });
        }


        //bootbox confirm
        function bootBoxConfirm(message,title,fun){
            bootbox.confirm({
                message: message,
                size: 'large',
                title: title,
                buttons: {
                    confirm: {
                        label: '<span class="glyphicon glyphicon-ok"></span>确定',
                        className: 'btn-success'
                    },
                    cancel: {
                        label: '<span class="glyphicon glyphicon-remove"></span>取消',
                        className: 'btn-danger'
                    }
                },
                callback: fun
            })
        }

    </script>
</body>
</html>
